package com.bridgeintelligent.tag.customer.service;

import com.bridgeintelligent.tag.customer.entity.CustomersChannel;
import com.bridgeintelligent.tag.customer.entity.CustomersPage;
import com.bridgeintelligent.tag.customer.mapper.ChannelMapper;
import com.bridgeintelligent.tag.customer.model.ChannelModel;
import com.bridgeintelligent.tag.customer.model.ChannelQueryModel;
import com.bridgeintelligent.tag.customer.vo.ChannelVO;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.utils.IdWorker;
import com.bridgeintelligent.tag.webserver.security.TagSecurityHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description
 * @author: grp 2021-02-21 23:19
 **/
@Service
@AllArgsConstructor
public class ChannelServiceImpl implements IChannelService{

    private final ChannelMapper channelMapper;
    @Override
    public int countById(String id) {
        return channelMapper.countById(id);
    }

    @Override
    public int countByName(String name) {
        return channelMapper.countByName(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ChannelModel model) {
        CustomersChannel channel = new CustomersChannel();
        BeanUtils.copyProperties(model, channel);
        channel.setId(IdWorker.nextId());
        channel.setIsDel("00");
        User user = TagSecurityHelper.currentUser();
        channel.setCreatorId(user.getUserId());
        channel.setCreateTime(new Date());
        channelMapper.add(channel);
    }

    @Override
    public int countByNameAndNotId(String channelId, String channelName) {
        return channelMapper.countByNameAndNotId(channelName, channelId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void put(ChannelModel model) {
        CustomersChannel channel = new CustomersChannel();
        BeanUtils.copyProperties(model, channel);
        channel.setUpdateTime(new Date());
        channelMapper.update(channel);
    }

    @Override
    public CustomersPage<ChannelVO> list(ChannelQueryModel model) {
        if (StringUtils.isNotEmpty(model.getStart())) {
            model.setStart(model.getStart() + " 00:00:00");
        }
        if (StringUtils.isNotEmpty(model.getEnd())) {
            model.setEnd(model.getEnd() + " 23:59:59");
        }
        Page page = PageHelper.startPage(model.getPageNumber(), model.getPageSize());
        List<ChannelVO> list = channelMapper.findList(model);
        return new CustomersPage<ChannelVO>().setContent(list).setTotalPages(page.getPages()).setRequestPager(model.getPageNumber())
                .setSize(model.getPageSize()).setTotalElements(page.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(String id) {
        CustomersChannel channel = new CustomersChannel();
        channel.setId(id);
        channel.setUpdateTime(new Date());
        channel.setIsDel("01");
        channelMapper.update(channel);
    }

    @Override
    public ChannelVO getDetail(String id) {
        return channelMapper.findOne(id);
    }

    @Override
    public List<ChannelVO> channels() {
        return channelMapper.findValidAll();
    }

    @Override
    public List<ChannelVO> allChannels() {
        return channelMapper.findAll();
    }
}
